<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-row>
        <el-col :span="15">
          <el-form-item label="应用名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="App名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="15">
          <el-form-item label="缩略图:" prop="thumb">
            <div class="myhead">
              <el-tooltip content="点击上传缩略图" placement="top">
                <el-image  style="width: 90px; height: 60px" :src="dataForm.thumb" fit="contain" @click="uploadThumbHandle()" alt=""/>
              </el-tooltip>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="图标机构" prop="origin">
            <el-input v-model="dataForm.origin" placeholder="类型组:比如微信，支付宝，新浪之类的"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="图标bundleId" prop="bundleId">
            <el-input v-model="dataForm.bundleId" placeholder="图标bundleId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="url" prop="url">
            <el-input v-model="dataForm.url" placeholder="App url 地址"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="系统图标" size="mini" prop="status">
            <el-radio-group v-model="dataForm.status">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <!-- 弹窗, 上传缩略图 -->
    <theme-upload-thumb v-if="uploadThumbVisible" ref="themeUploadThumb" @refreshDataList="refreshUploadThumb"></theme-upload-thumb>
  </el-dialog>
</template>

<script>
  import 'babel-polyfill'
  // 上传缩略图
  import ThemeUploadThumb from './theme-upload-image'

  export default {
    data () {
      return {
        visible: false,
        uploadThumbVisible: false,
        dataForm: {
          id: 0,
          name: '',
          origin: '',
          bundleId:'',
          url: '',
          status: '1',
          thumb: 'http://file-cdn-theme.cykonhy.com/default/default.jpg',
        },
        dataRule: {
          name: [
            { required: true, message: 'App名称不能为空', trigger: 'blur' }
          ],
          origin: [
            { required: true, message: '图标组:比如微信，支付宝，新浪之类的不能为空', trigger: 'blur' }
          ],
          bundleId: [
            { required: true, message: 'App图标地址不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: 'App图标地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      ThemeUploadThumb,
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/theme/app-icon/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.origin = data.appIcon.origin
                this.dataForm.name = data.appIcon.name
                this.dataForm.bundleId = data.appIcon.bundleId
                this.dataForm.url = data.appIcon.url
                this.dataForm.status = data.appIcon.status
                this.dataForm.thumb = data.appIcon.thumb
              }
            })
          }
        })
      },
      // 上传缩略图
      uploadThumbHandle () {
        this.uploadThumbVisible = true
        this.$nextTick(() => {
          this.$refs.themeUploadThumb.init()
        })
      },
      refreshUploadThumb (filePath) {
        if (filePath != ""){
          this.dataForm.thumb = filePath
        }
        this.uploadThumbVisible = false
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/theme/app-icon/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'origin': this.dataForm.origin,
                'name': this.dataForm.name,
                'bundleId': this.dataForm.bundleId,
                'url': this.dataForm.url,
                'status': this.dataForm.status,
                'thumb': this.dataForm.thumb,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
